package com.ls.fw.index.test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class LuceneTest36 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		// createIndexFile();
		searchIndexFileResult();
	}

	public static void createIndexFile() {
		IndexWriter indexWriter = null;
		try {
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);
			IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
					Version.LUCENE_45, analyzer);
			Directory directory = new SimpleFSDirectory(new File("./index"));
			indexWriter = new IndexWriter(directory, indexWriterConfig);
			Article article = new Article(11, "最xx的城市", "xx");
			Document doc = new Document();
			doc.add(new Field("id", article.getId() + "", Store.YES,
					Index.ANALYZED));
			doc.add(new Field("title", article.getTitle().toString(),
					Store.YES, Index.ANALYZED));
			doc.add(new Field("content", article.getContent().toString(),
					Store.YES, Index.ANALYZED));
			indexWriter.addDocument(doc);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (indexWriter != null) {
				try {
					indexWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	// 如果查询是需要用到解析器，那解析器必须和创建时的解析器相同
	public static void searchIndexFileResult() throws IOException {
		List<Article> articles = new ArrayList<Article>();
		// 得到索引的目录
		Directory directory = new SimpleFSDirectory(new File("./index/"));
		// 根据目录打开一个indexReader
		IndexReader indexReader = IndexReader.open(directory);
		// System.out.println(indexReader.maxDoc());
		// 获取最小值的document对象
		// Document doc=indexReader.document(0);
		// 获取最大值的document对象
		// Document doc=indexReader.document(indexReader.maxDoc()-1);
		// document对象的get(字段名称)方法获取字段的值
		/*
		 * System.out.println(doc.get("id"));
		 * System.out.println(doc.get("title"));
		 * System.out.println(doc.get("content"));
		 */
		int n = indexReader.maxDoc();
		System.out.println(n);
		for (int i = 0; i < n; i++) {
			Document doc = indexReader.document(i);
			Article article = new Article();
			if (doc.get("id") == null) {
				System.out.println("id为空");
			} else {
				article.setId(Integer.parseInt(doc.get("id")));
				article.setTitle(doc.get("title"));
				article.setContent(doc.get("content"));
				articles.add(article);
			}
		}
		for (Article article : articles) {
			System.out.println(article.toString());
		}
	}
}

class Article {
	private int id;
	private String title;
	private String content;

	public Article() {
		super();
	}

	public Article(int id, String title, String content) {
		this.id = id;
		this.title = title;
		this.content = content;
	}

	public int getId() {
		return id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return id + title + content;
	}
}
